﻿<%@ Page Title="" Language="C#" MasterPageFile="~/WithoutPermBasic.Master" AutoEventWireup="true" CodeBehind="StorageInDetail.aspx.cs" Inherits="StorageManage.UI.StorageInDetail" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
     <script type="text/javascript">
         $(function () {
             var height = $("#divTotal").height();
             var master = $("#master").height();
             var content = parseFloat(height) - parseFloat(master) - 24;
             $("#tbList").css("height", content);
         });
     </script>
    <script type="text/javascript">
        $(function () {
            var inCode = com.get.inCode;
            var action = com.get.action;
            if (action=='add') {
                $('#ApplyName').val('<%=applyName%>');
                $('#ApplyTime').val('<%=applyTime%>');
            }
            $('#InCode').val(inCode);
            $('#TaskCode').combobox({
                valueFiled: 'value',
                textField: 'text',
                url: '/Logic/StorageIn.aspx?method=GetTaskCode',
                mode: 'remote',
                panelHeight: 200,
                required:true,
                onSelect: function (rec) {
                    com.ajax({
                        url: '/Logic/StorageIn.aspx?method=GetProjAndEng',
                        data: { taskCode: rec.text },
                        required:true,
                        success: function (data) {
                            $('#ProjName').val(data.projName);
                            $('#EngName').val(data.engName);
                        }
                    });
                }

            });
            var columnArr = [{
                field: 'ck', checkbox: true, width: 30, align: 'center',
            },
            {
                field: 'HmCode', title: '助记码', width: 110, align: 'center',
                formatter: function (value, row, index) {
                    if (value==undefined) {
                        value = "";
                    }
                    return '<input  name="HmCode" id="HmCode" class="easyui-combogrid" style="width:120px" value="' + value + '" />'
                }
            },

                      {
                          field: 'MaterialCode', title: '物料编码', width: 110, align: 'center',
                          formatter: function (value, row, index) {
                              if (value == undefined) {
                                  value = "";
                              }
                              return '<input  name="MaterialCode" id="MaterialCode" style="width:100px" readonly="true" value="' + value + '" />'
                          }
                      },
                      {
                          field: 'Name', title: '物料名称', width: 120, align: 'center',
                          formatter: function (value, row, index) {
                              if (value == undefined) {
                                  value = "";
                              }
                              return '<input  name="Name" id="Name" readonly="true" style="width:110px" value="' + value + '"/>';
                          }
                      },
                      {
                          field: 'Guige', title: '规格型号', width: 110, align: 'center',
                          formatter: function (value, row, index) {
                              if (value == undefined) {
                                  value = "";
                              }
                              return '<input  readonly="true"   name="Guige" id="Guige"  style="width:100px" value="' + value + '"/>';
                          }
                      },
                      {
                          field: 'Caizhi', title: '材质', width: 110, align: 'center',
                          formatter: function (value, row, index) {
                              if (value == undefined) {
                                  value = "";
                              }
                              return '<input  readonly="true"  name="Caizhi" id="Caizhi"  style="width:100px" value="' + value + '"/>';
                          }
                      },
                      {
                          field: 'Mweight', title: '理论重量', width: 80, align: 'center',
                          formatter: function (value, row, index) {
                              if (value == undefined) {
                                  value = "";
                              }
                              return '<input  readonly="true"  name="Mweight" id="Mweight"  style="width:70px" value="' + value + '"/>';
                          }
                      },
                      {
                          field: 'Length', title: '长', width: 70, align: 'center',
                          formatter: function (value, row, index) {
                              if (value == undefined) {
                                  value = "";
                              }
                              return '<input  name="Length" id="Length" style="width:60px" value="' + value + '"/>';
                          }
                      },
                      {
                          field: 'Width', title: '宽', width: 70, align: 'center',
                          formatter: function (value, row, index) {
                              if (value == undefined) {
                                  value = "";
                              }
                              return '<input  name="Width" id="Width" style="width:60px"  value="' + value + '"/>';
                          }
                      },
                       
                      {
                          field: 'ZhangNum', title: '总支/张', width: 60, align: 'center',
                          formatter: function (value, row, index) {
                              if (value == undefined) {
                                  value = "";
                              }
                              return '<input name="ZhangNum" id="ZhangNum" style="width:50px" value="' + value + '"/>';
                          }
                      },
                       {
                           field: 'TotalWeight', title: '总重', width: 80, align: 'center',
                           formatter: function (value, row, index) {
                               if (value == undefined) {
                                   value = "";
                               }
                               return '<input name="TotalWeight" id="TotalWeight" style="width:70px" value="' + value + '"/>';
                           }
                       },
                      {
                          field: 'PurcUnit', title: '单位', width: 55, align: 'center',
                          formatter: function (value, row, index) {
                              if (value == undefined) {
                                  value = "";
                              }
                              return '<input  readonly="true"  name="PurcUnit" id="PurcUnit" readonly="true" style="width:45px" value="' + value + '"/>';
                          }
                      },
               {
                   field: 'MaterialType', title: '物料类型', align: 'center', width: 80,
                   formatter: function (value, row, index) {
                       if (value == undefined) {
                           value = "";
                       }
                       return '<input  name="MaterialType" id="MaterialType" style="width:70px" value="' + value + '"/>';
                   }
               },
               {
                   field: 'Note', title: '备注', width: 120, align: 'center',
                   formatter: function (value, row, index) {
                       if (value == undefined) {
                           value = "";
                       }
                       return '<input class="easyui-textbox" name="Note" id="Note" style="width:170px" value="' + value + '"/>';
                   }
               },
                {
                    field: 'Id', title: 'Id', width: 120, align: 'center',hidden:true,
                    formatter: function (value, row, index) {
                        if (value == undefined ||value==null) {
                            value = 0;
                        }
                        return '<input class="easyui-textbox" name="Id" id="Id"  value="' + value + '"/>';
                    }
                },

            ];
            //如果是添加入库数据
            if (action=='add') {
                $('#tbList').datagrid({
                    rownumbers: true,
                    singleSelect: true,
                    columns: [
                        columnArr
                    ],

                });
                for (i = 0; i < 10; i++) {
                    $('#tbList').datagrid('insertRow', {
                        index: i,	// 索引从0开始
                        row: {
                        },
                    });
                };
                uiLoad();
            }
                //如果是编辑入库数据
            else {
                com.ajax({
                    url: '/Logic/StorageIn.aspx?method=GetStorageEdit&inCode=' + inCode,
                    success: function (data) {                      
                        $('#form1').form('load', data.main);
                        $('#tbList').datagrid({
                            data: data.detail,
                            idField:'Id',
                            rownumbers: true,
                            singleSelect: true,
                            columns: [
                                columnArr
                            ],
                            onLoadSuccess: function () {
                                for (var i = 0; i < 10 - data.detail.length; i++) {
                                    $('#tbList').datagrid('insertRow', {
                                        index: data.detail.length+i,	// 索引从0开始
                                        row: {
                                        },
                                    });
                                }
                                uiLoad();
                               
                            }
                        });
                    }
                });

               
            }
            
          
            
        })
    </script>
    <script type="text/javascript">
        function uiLoad() {
            $('input[name=MaterialType]').combobox({
                valueField: 'text',
                textField: 'text',
                data: StaticCombo.MaterType,
                panelHeight: 'auto',
                panelWidth: 70,
                editable: false,
            });

            $('input[name=HmCode]').combogrid({
                delay: 500,
                mode: 'remote',
                idField: 'Id',
                textField: 'HmCode',
                panelWidth: 420,
                loadMsg: '数据正在加载中，请稍后...',
                pagination: true,
                rownumbers: true,
                hasDownArrow: false,
                pageSize: 8,
                pageList: [8, 15, 20, 30],
                url: '/Logic/StorageIn.aspx?method=GetMaterialMsg',
                columns: [[
                    { field: 'HmCode', title: '助记码', width: 80, align: 'center' },
                    { field: 'Material', title: '物料编码', width: 100, align: 'center' },
                    { field: 'Name', title: '物料名称', width: 120, align: 'center' },
                    { field: 'Guige', title: '物料规格', width: 80, align: 'center' },
                    { field: 'Caizhi', title: '物料材质', width: 80, align: 'center' },
                    { field: 'PurcUnit', title: '单位', width: 20, align: 'center' },
                    { field: 'Mweight', title: '理论重量', width: 50, align: 'center' }
                ]],
                onSelect: function () {
                    var row = $('#tbList').datagrid('getSelected');
                    var index = $('#tbList').datagrid('getRowIndex', row);
                    var $tr = $('#divTotal .datagrid-view2 .datagrid-body').find('tr').eq(index);
                
                    var g = $tr.find('#HmCode').combogrid("grid");
                    var r = g.datagrid('getSelected');	// get the selected row
                    console.log(r);
                    $tr.find('input[name=MaterialCode]').val(r.Material);
                    $tr.find('input[name=Name]').val(r.Name);
                    $tr.find('input[name=Guige]').val(r.Guige);
                    $tr.find('input[name=Caizhi]').val(r.Caizhi);
                    $tr.find('input[name=PurcUnit]').val(r.PurcUnit);
                    $tr.find('#Mweight').val(r.Mweight);
                    if (r.Material.indexOf('0101')==0 ||r.Name.indexOf('板')>=0) {
                        $tr.find('input[textboxname=MaterialType]').combobox('setValue', '非定尺板');
                    }
                    else if (r.Material.indexOf('0103') == 0 || r.Material.indexOf('0102')==0) {                     
                        $tr.find('input[textboxname=MaterialType]').combobox('setValue', '型材');
                    }
                }
                    
                
            });
            $('input[name=ZhangNum]').numberbox({
                min: 0,
                onChange: function (newValue, oldValue) {
                    $tr = $(this).closest('tr');
                    var length = utils.ParseFloat($tr.find('#Length').numberbox('getValue'));
                    
                    var width =utils.ParseFloat($tr.find('#Width').numberbox('getValue'));
                    var theoryWeight = utils.ParseFloat($tr.find('#Mweight').val());              
                    if (theoryWeight==undefined || theoryWeight=='') {
                        theoryWeight = 0;
                    }
                    var materialType = $tr.find('#MaterialType').combobox('getValue');
                    if (materialType.indexOf('板')>0) {
                        $tr.find('#TotalWeight').numberbox('setValue', (newValue * length*width*theoryWeight).toFixed(3))
                    } else if (materialType=='型材') {
                        $tr.find('#TotalWeight').numberbox('setValue', (newValue * length*theoryWeight/1000).toFixed(3))
                    }
                   
                }
            });
            $('input[name=Length]').numberbox({
                min:0,
                precision: 3,
                onChange: function (newValue,oldValue) {
                    $tr = $(this).closest('tr');
                    var width = utils.ParseFloat($tr.find('#Width').numberbox('getValue'));
                    var zhangNum = utils.ParseInt($tr.find('#ZhangNum').numberbox('getValue'));
                    var theoryWeight = utils.ParseFloat($tr.find('#Mweight').val());
                    if (theoryWeight == undefined || theoryWeight == '') {
                        theoryWeight = 0;
                    }      
                    var materialType = $tr.find('#MaterialType').combobox('getValue');
                    if (materialType.indexOf('板') > 0) {
                        $tr.find('#TotalWeight').numberbox('setValue', (utils.ParseFloat(newValue) * zhangNum * width * theoryWeight).toFixed(3))
                    } else if (materialType == '型材') {
                        $tr.find('#TotalWeight').numberbox('setValue', (utils.ParseFloat(newValue) * zhangNum * theoryWeight / 1000).toFixed(3))
                    }
                }
            });
            $('input[name=Width]').numberbox({
                min: 0,
                precision: 3,
                onChange: function (newValue, oldValue) {
                    $tr = $(this).closest('tr');
                    var length = utils.ParseFloat( $tr.find('#Length').numberbox('getValue'));
                    var zhangNum =utils.ParseInt($tr.find('#ZhangNum').numberbox('getValue'));
                    var theoryWeight = utils.ParseFloat($tr.find('#Mweight').val());                  
                    if (theoryWeight == undefined || theoryWeight == '') {
                        theoryWeight = 0;
                    }
                    var materialType = $tr.find('#MaterialType').combobox('getValue');
                    if (materialType.indexOf('板') > 0) {
                        $tr.find('#TotalWeight').numberbox('setValue', (utils.ParseFloat(newValue) * zhangNum * length * theoryWeight).toFixed(3))
                    } 
                }
            });
            $('input[name=TotalWeight]').numberbox({
                min: 0,
                precision: 4
            });

           
        }
        function funcSave() {
            var form = com.serializeObj('form1');
            var rows = $('#tbList').datagrid('getRows');
            var array = new Array();
            for (var i = 0; i < rows.length; i++) {
                var $tr = $('#divTotal .datagrid-view2 .datagrid-body').find('tr').eq(i);
                var row = {};
                row.MaterialCode = $tr.find('#MaterialCode').val();
                row.Length = $tr.find('#Length').val();
                row.Width = $tr.find('#Width').val();
                //row.ZhangWeight = $tr.find('#ZhangWeight').numberbox('getValue');
                row.ZhangNum = $tr.find('#ZhangNum').numberbox('getValue');
                row.TotalWeight = $tr.find('#TotalWeight').numberbox('getValue');
                row.MaterialType = $tr.find('#MaterialType').combobox('getValue');
                row.Note = $tr.find('input[name=Note]').val();
                row.Id = $tr.find('input[name=Id]').val();
                if (row.MaterialCode != null && row.MaterialCode != "") {
                    array.push(row);
                }
            }
            $.extend(form, { Info: JSON.stringify(array) });
            com.ajax({
                url: '/Logic/StorageIn.aspx?method=SaveStorageIn&action='+com.get.action,
                data: form,
                success: function (data) {
                    if (data.state == "success") {
                        com.message('information', data.msg);     
                        if (com.get.action=='add') {
                            top.wrapper.tabClosethisRefreshByTitle('入库管理')
                        }
                    }

                }
            });
            
        }
        function funcAdd() {
            var rows = $('#tbList').datagrid('getRows');
            $('#tbList').datagrid('insertRow', {
                index: rows.length,
                row: {}
            });
            uiLoad();
        }
        function funcRemove() {
            var row = $('#tbList').datagrid('getSelected');
            if (row) {
                var index = $('#tbList').datagrid('getRowIndex', row);
                $('#tbList').datagrid('deleteRow', index);
                com.message('information', '删除成功')
            } else {
                com.message('information', '请选中要删除的行')
            }

        }
    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceToorbar" runat="server">
     <a href="javascript:void(0)" id="btnSave" class="easyui-linkbutton" onclick="funcSave()"
        data-options="iconCls:'icon-save'">保存</a>
    <a href="javascript:void(0)" id="btnAdd" class="easyui-linkbutton" onclick="funcAdd()"
        data-options="iconCls:'icon-add'">新增行</a>
    <a href="javascript:void(0)" id="btnRemove" class="easyui-linkbutton" onclick="funcRemove()"
        data-options="iconCls:'icon-remove'">删除行</a>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder2" runat="server">
    <div id="divTotal" style="height:100%;width:100%">
            
            <form id="form1">
                <table style="width:100%">
                    <tr >
                        <th colspan="8">
                            <h1>余料入库单</h1>
                        </th>
                    </tr>
                    <tr>
                        <th colspan="8">
                                <hr />
                        </th>
                    </tr>
                    <tr>
                        <td style="text-align:right;">入库编号：</td>
                        <td>
                            <input type="text" name="InCode" id="InCode" style="border:none;"  readonly="readonly"/>
                            <input type="hidden" name="Id" id="Id"/>
                        </td>
                         <td style="text-align:right;">制单人：</td>
                        <td> 
                            <input type="text" name="ApplyName" id="ApplyName"  style="border:none;" readonly="readonly"/>
                        </td>
                         <td style="text-align:right;">制单时间：</td>
                        <td>
                            <input type="text" name="ApplyTime" id="ApplyTime"  style="border:none;" readonly="readonly"/>
                        </td>
                         <td style="text-align:right;">入库班组：</td>
                        <td>
                            <input id="InTeam" name="InTeam" class="easyui-combobox" data-options="valueField:'text',textField:'text',url:'/Logic/StorageIn.aspx?method=GetTeam',panelHeight:'200',mode:'remote',required:'true'," />
                        </td>
                    </tr>

                    <tr>
                        <td style="text-align:right;">生产制号：</td>
                        <td>
                            <input type="text" name="TaskCode" id="TaskCode" />
                        </td>
                         <td style="text-align:right;">项&nbsp;&nbsp;&nbsp;&nbsp;目：</td>
                        <td>
                            <input type="text" name="ProjName" id="ProjName" readonly="readonly" />
                        </td>
                         <td style="text-align:right;">工&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;程：</td>
                        <td>
                            <input type="text" name="EngName" id="EngName" readonly="readonly" />
                        </td>
                         <td style="text-align:right;">仓&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;库：</td>
                        <td>
                            <input type="text" name="Warehouse" id="Warehouse"  class="easyui-combobox" data-options="valueField:'text',textField:'text',url:'/Logic/BasicInfo.aspx?method=GetWarehouse',panelHeight:'200',mode:'remote',required:'true'," />
                        </td>
                    </tr>
                    <tr>
                        <td style="text-align:right;">备&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;注：</td>
                <td ">
                    <input type="text" name="Note" id="Note" style="width:200px;" />
                </td>
                    </tr>
                </table>
            </form>
        <table id="tbList" title="余料入库表" style="width:100%"  >
        </table>
        </div>
        
    
    
</asp:Content>
